package com.ddlc.javademo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.ddlc.anno.StudentInfo;

import java.lang.reflect.Field;
import java.lang.reflect.Method;


/**
 * https://gitee.com/tangbuzhi
 *
 * @author: Tangbuzhi
 * @version:
 * @package:
 * @description:
 * @modify:
 * @date: 2018/4/19
 */

public class StudentActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student);
        Student student = new Student();
        processField(student);
        processMethod(student);
    }

    private void processField(Student student) {
        Class<?> clz = student.getClass();
        Field[] declaredFields = clz.getDeclaredFields();
        for (Field field : declaredFields) {
            StudentInfo annotation = field.getAnnotation(StudentInfo.class);
            if (annotation != null) {
                String name = annotation.name();
                int age = annotation.age();
                StudentInfo.Gender gender = annotation.gender();
                String address = annotation.address();
                System.out.println(field.getName() + " ==> " + name + " ==> " + age + " ==> " + gender.toString() + " ==> " + address);
            }
        }
    }

    private void processMethod(Student student) {
        Class<?> clz = student.getClass();
        Method[] declaredMethods = clz.getDeclaredMethods();
        for (Method method : declaredMethods) {
            StudentInfo annotation = method.getAnnotation(StudentInfo.class);
            if (annotation != null) {
                String name = annotation.name();
                int age = annotation.age();
                StudentInfo.Gender gender = annotation.gender();
                String address = annotation.address();
                System.out.println(method.getName() + " ==> " + name + " ==> " + age + " ==> " + gender.toString() + " ==> " + address);
            }
        }
    }
}
